package business

import model.Direction
import model.IView

/**
 * created by lucus on 2019/4/4
 * e-mail: mcphilex1989@gmail.com
 * phone: 15652608401
 */
interface Moveable : IView {

    val mCurrentDirection: Direction

    val speed: Int

    /**
     *  移动物体与阻挡物体发生碰撞
     *  @return Direction 返回碰撞的方向
     */
    fun willCollision(blockable: Blockable): Direction? {
        // 判断坦克将要移动的位置是否发生碰撞
        var x = this.x
        var y = this.y

        when (this.mCurrentDirection) {
            Direction.UP -> y -= speed
            Direction.DOWN -> y += speed
            Direction.LEFT -> x -= speed
            Direction.RIGHT -> x += speed
        }

        // 判断越界处理，返回越界的方向
        if (x < 0) return Direction.LEFT
        if (x > BaseConfig.GAME_WINDOW_WIDTH - width) return Direction.RIGHT
        if (y < 0) return Direction.UP
        if (y > BaseConfig.GAME_WINDOW_HEIGHT - height) return Direction.DOWN

        val collision = checkCollision(x, y, width, height, blockable.x, blockable.y, blockable.width, blockable.height)


        return if (collision) mCurrentDirection else null
    }

    fun notifyCollision(direction: Direction?, blockable: Blockable?)

}